use List::Util 'shuffle';
require 'doctorIO.pl';
my $datepath = "dates/";
sub make_day {
	clear("$datepath$_[0]");
}
#get a list of doctors and types
sub get_choices {
	my @choices = ();
	foreach my $doc (get_doctors()){
		push @choices, "d,$doc";
	}
	foreach my $type (get_types()){
		push @choices, "t,$type";
	}
	return @choices;
}
#cancel a reservation
sub cancel_doctor {
	my ($doctor, $user, $date, $time) = @_;
	system("./stats.pl -c '$user'");
	open(IN, "<", "$datepath$date");
	my @lines = grep !/$doctor,$user,$time/, <IN>;
	close(IN);
	open(OUT, ">", "$datepath$date");
	print OUT @lines;
	close(OUT);
}
#make an appointment with a specific doctor
sub appointment_doctor {
	my ($doctor, $user, $date, $time) = @_;
	my @times = get_times_doctor($date, $doctor);
	if ($times[$time-9] == 0){
		system("echo $doctor,$user,$time >> $datepath$date");
		system("./stats.pl -r '$user' '$doctor'");
		alog("$user made an appointment with $doctor at $time, $date");
		return 1;
	}
	else {
		return 0;
	}
}
#make a random appointment with an available doctor of some type
sub appointment_type {
	my ($type, $user, $date, $time) = @_;
	my @docs = get_doctors($type);
	shuffle(@docs);
	my $found = 0;
	foreach my $doc (@docs){
		if (appointment_doctor($doc, $user, $date, $time)){
			$found = $doc;
			last;
		}
	}
	return $found;
}
#get a user's current appointments for the day
#1 means not reserved, 2 means reserved
sub get_reserves {
	my ($date, $doctor, $user) = @_;
	my @available = (1,1,1,1,1,1,1,1);
	my @vals = get_doctor($doctor);
	for (my $i = $vals[2]; $i < $vals[3]; $i++){
		$available[$i-9] = 0;
	}
	open (DAY, "$datepath$date");
	while (<DAY>){
		my @vals = tokenize($_);
		if ($vals[0] eq $doctor){
			$available[$vals[2]-9] = $vals[1] eq $user ? 2 : 1;
		}
	}
	return @available;
}
#get_times() works with get_choices()
sub get_times {
	my ($flag, $date, $val) = @_;
	if (not -e "$datepath$date"){
		clear("$datepath$date");
	}
	return $flag eq "d" ? get_times_doctor($date, $val) : get_times_type($date, $val);
}
#similar to get_reserves, except 0 means free
sub get_times_doctor {
	my ($date, $doctor) = @_;
	my @available = (1,1,1,1,1,1,1,1);
	my @vals = get_doctor($doctor);
	for (my $i = $vals[2]; $i < $vals[3]; $i++){
		$available[$i-9] = 0;
	}
	open (DAY, "$datepath$date");
	while (<DAY>){
		my @vals = tokenize($_);
		if ($vals[0] eq $doctor){
			$available[$vals[2]-9] = 1;
		}
	}
	return @available;
}
#this is just a logical AND of all doctors of that type
sub get_times_type {
	my ($date, $type) = @_;
	my @times = (1,1,1,1,1,1,1,1);
	foreach my $doctor (get_doctors_type($type)){
		my @dtimes = get_times_doctor($date, $doctor);
		for (my $i = 0; $i < 8; $i++){
			$times[$i] = $times[$i]&$dtimes[$i];
		}
	}
	return @times;
}
#this makes a row in the table for the PDF generation
sub make_row {
	my ($date,$doctor) = (date(), $_[0]);
	my @fields = map "\\#\\#\\#", 1..8;
	my @vals = get_doctor($doctor);
	for (my $i = $vals[2]; $i < $vals[3]; $i++){
		$fields[$i-9] = " ";
	}
	open (DAY, "$datepath$date");
	while (<DAY>){
		my @vals = tokenize($_);
		if ($vals[0] eq $doctor){
			$fields[$vals[2]-9] = $vals[1];
		}
	}
	my $text = $doctor;
	foreach my $field (@fields){
		$text .= " & " . get_name($field);
	}
	return $text;
}
sub get_name {
	my $email = $_[0];
	if ($email eq "\\#\\#\\#"){
		return $email;
	}
	open PASS, "password.txt";
	my @lines = grep /$email,/, <PASS>;
	my @fields = split /,/, $lines[0];
	return $fields[2];	
}
1;
